const puppeteer = require('puppeteer');
const log = require('./utils/log')
const autoScroll = require('./utils/autoScroll')
const batchDownload = require('./utils/downloadPics')

puppeteer.launch().then(async browser => {

    let key = 1

    while(key < 42){
        let sd = key
        if(key<10) {
            sd = '00' + key
        }else {
            sd = '0' + key
        }
        const page = await browser.newPage();        
        await page.waitFor(2000)
        await page.goto(`https://nudegirls4u.com/girlt-no-${sd}/`,{
            waitUntil: 'networkidle2',
            timeout: 100000
        })
        
        let name = ''

        if (await page.$('.entry-content p:nth-child(2) a')) {
            name = await page.evaluate(el => el.innerHTML, await page.$('.entry-content p:nth-child(2) a'))
        } else if (await page.$('.entry-content p:nth-child(2)')) {
            name = await page.evaluate(el => el.innerHTML, await page.$('.entry-content p:nth-child(2)'))
        } else {
            log(`${key}\n`)
            key++
            continue
        }

        name = name.replace('Model: ', '')
        name = name.replace('Model:', '')
        name = name.replace(/&nbsp;/g, '')

        if (name.search('<img') !== -1) {
            log(`${key}\n`)
            key++
            continue
        } else if (name.search('<b>') !== -1) {
            log(`${key}\n`)
            key++
            continue
        }

        await page.waitFor(3000)

        const srcs = await page.evaluate(() => {
            const images = document.querySelectorAll('.rgg-imagegrid a')
            return Array.prototype.map.call(images, a => a.href);
        })

        let nameKey = key

        if (nameKey<10) {
            nameKey = '000' + nameKey
        }else{
            nameKey = '00' + nameKey
        }

        console.log(name)

        batchDownload(srcs, `${nameKey}-${name}`)

        await page.close()
        key++

    }
});





